tongfusoft.account = function() {
}

tongfusoft.account.prototype = {
	accountTable :function(){
		var table = new tongfusoft.table();
		var columns = [
				{
					"data" : "accountId"
				},
				{
					"data" : "accountName"
				},
				{
					"data" : "loginName"
				},
				{
					"data" : "lastUpdDate"
				},
				{
					"data" : "accountId",
					"render" : function(data, type, full, meta) {
						var delbut = '<button accountId="'
								+ data
								+ '" onclick="new tongfusoft.account().accountDel(this)" type="button" data-toggle="modal" data-target="#delaccount" class="detBtn btn btn-default btn-sm "><em class="fa fa-trash"></em></button>'
						var editbut = '<button accountId="'
								+ data
								+ '" onclick="new tongfusoft.account().accountEdit(this)" type="button" data-toggle="modal" data-target="#delaccountClass" class="detBtn btn btn-default btn-sm "><em class="fa fa-edit"></em></button>'

						return editbut + delbut;
					}

				} ]

		table.dataTable('accountTable', 'account/query', columns, null);

	},	
		
	//新增账户
	addBtnAccount : function() {
		$("#addBtnAccount").click(function() {
			$("#accountModelTitle").text('新增');
			common.clean("accountForm");
			$('#accountModel').modal('show');
			$("#oper").val("add");

		})

	},
	//删除账户
	accountDel :function(obj){
		var flag = false;
		common.post('isAccountHasUsers', {
			"accountId" : $(obj).attr("accountId")
		}, function(data) {
			if (data.state == 'success' && data.data != 0) {
				flag = true;
			}
		});
		if(flag==true){
			toastr["error"]('该账户已绑定用户，无法删除!');
		}else{
			swal({
				title : "是否确认?",
				text : "",
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "删除",
				cancelButtonText : "取消",
				closeOnConfirm : true
			}, function() {
				common.ajaxSubmitOnSuccess({
					"accountId" : $(obj).attr("accountId")
				}, 'account/del', function(data) {

					if (data.state == 'success') {

						$('#accountTable').DataTable().ajax.reload();
					}

				}, "del")
			});

		}
		
	},
	//编辑账户
	accountEdit : function(obj) {

		common.post('account/detail', {
			"accountId" : $(obj).attr("accountId")
		}, function(data) {
			$("#accountModelTitle").text('修改')
			$('#accountModel').modal('show');
			$("#oper").val("edit");
			if (data.state == 'success') {
				$('#accountName').val(data.data.accountName);
				$('#accountId').val(data.data.accountId);
			}

		});

	},
	 //提交submit
	accountSubmit :function(){
		$.validator.setDefaults({
			submitHandler : function() {
				var initData = common.getData("accountForm");
				var url = 'account/add';

				if ($("#oper").val() == 'edit') {

					url = 'account/update';
					initData['accountId'] = $("#accountId").val();
				}
				common.ajaxSubmitOnSuccess(initData, url, function(data) {

					if (data.state == 'success') {

						$('#accountModel').modal('hide');
						$('#accountTable').DataTable().ajax.reload();

					} else if (data.code == 'repeat_error') {

						toastr["error"]('账户名不能重复!');
					}

				}, $("#oper").val())
			}
		});
		$("#accountForm").validate();

		$("#submitBtn").click(function() {

			$("#accountForm").submit();
		})
	},
}

$(function() {

	var account = new tongfusoft.account();
	
	account.accountTable();
	account.addBtnAccount();
	account.accountSubmit();
	
})